// pages/evaluate/evaluate.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsid: null,
    orderid: null,
    recid: null,
    statArr: [1, 2, 3, 4, 5],
    sellerSum: 5, //卖家服务
    sellerEvaluateClass: '超赞',
    sellerBg: '#D81E06',
    logisticsSum: 5, //物流服务
    logisticsEvaluateClass: '超赞',
    logisticsBg: '#D81E06',
    describeSum: 5, //描述服务
    describeEvaluateClass: '超赞',
    describeBg: '#D81E06',
    goodsSum: 5, //商品评价
    goodsEvaluateClass: '超赞',
    goodsBg: '#D81E06',
    placeInput: '',
    addImg: [],
    addImgBtn: false,
    checked: false,
    radioChange: 1,
    imgSum: 5,
    imgList: [], //上传图片列表
    imgListSum: null, //最后一次上传图片个数
    isRemoveImg: true, //是否删除过图片
    imgCenter: [] //删除过图片之后图片保存
  },
  // 点击卖家服务星星评价
  sellerStarClick(e) {
    const that = this;
    let sum = e.currentTarget.dataset.index + 1;
    that.setData({
      sellerSum: sum
    })
    that.select(sum, 'sellerEvaluateClass', 'sellerBg');
  },
  // 点击物流服务星星评价
  logisticsStarClick(e) {
    const that = this;
    let sum = e.currentTarget.dataset.index + 1;
    that.setData({
      logisticsSum: sum
    })
    that.select(sum, 'logisticsEvaluateClass', 'logisticsBg');
  },
  // 点击描述服务星星评价
  describeClick(e) {
    const that = this;
    let sum = e.currentTarget.dataset.index + 1;
    that.setData({
      describeSum: sum
    })
    that.select(sum, 'describeEvaluateClass', 'describeBg');
  },
  // 点击商品评价星星评价
  goodsClick(e) {
    const that = this;
    let sum = e.currentTarget.dataset.index + 1;
    that.setData({
      goodsSum: sum
    })
    that.select(sum, 'goodsEvaluateClass', 'goodsBg');
  },
  select(sum, evaluateClass, bg) {
    const that = this;
    switch (sum) {
      case 1:
        that.setData({
          [evaluateClass]: '非常差',
          [bg]: '#B3B3B3'
        })
        return false;
      case 2:
        that.setData({
          [evaluateClass]: '差',
          [bg]: '#B3B3B3'
        })
        return false;
      case 3:
        that.setData({
          [evaluateClass]: '一般',
          [bg]: "#FECD44"
        })
        return false;
      case 4:
        that.setData({
          [evaluateClass]: '满意',
          [bg]: "#FECD44"
        })
        return false;
      case 5:
        that.setData({
          [evaluateClass]: '超赞',
          [bg]: "#D81E06"
        })
        return false;
    }
  },
  //获取输入内容
  bindReplaceInput(e) {
    const that = this;
    that.setData({
      placeInput: e.detail.value
    })
  },
  // 添加图片
  addImg() {
    const that = this;
    var imgSum = that.data.imgSum;
    wx.chooseImage({
      count: imgSum,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        let imgUrl = [];
        that.data.addImg.forEach((data) => {
          imgUrl.push(data)
        })
        tempFilePaths.forEach((data) => {
          imgUrl.push(data)
        })
        imgSum -= tempFilePaths.length;
        if (imgSum <= 0) {
          that.setData({
            addImgBtn: true
          })
        }
        that.setData({
          addImg: imgUrl,
          imgSum: imgSum
        })
        var pics = that.data.addImg;
        that.uploadimg({
          url: app.api.reqUrls.uploadFile, //这里是你图片上传的接口
          path: pics //这里是选取的图片的地址数组
        });
       
      }
    })
  },
  // 删除图片
  removeImg(e) {
    const that = this;
    var imgSum = that.data.imgSum;
    const imgListLength = that.data.imgList.length;
    const imgStart = imgListLength - (that.data.imgListSum + 1);
    let imgCenter = that.data.imgList.slice(imgStart, imgListLength);
    wx.showModal({
      title: '提示',
      content: '是否删除',
      success(res) {
        if (res.confirm) {
          let imgUrl = that.data.addImg;
          let sum = e.currentTarget.dataset.index;
          imgCenter.splice(sum, 1)
          imgUrl.splice(sum, 1);
          imgSum++;
          that.setData({
            addImg: imgUrl,
            imgSum: imgSum,
            addImgBtn: false,
            imgCenter: imgCenter,
            isRemoveImg: false
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }

      }
    })
  },
  // 是否匿名
  radioChange(e) {
    const that = this;
    that.setData({
      checked: !that.data.checked
    })
    if (that.data.checked) {
      that.setData({
        radioChange: 0
      })
    } else {
      that.setData({
        radioChange: 1
      })
    }
  },
  //多张图片上传
  uploadimg(data) {
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0; //上传失败的个数
    let imgList = that.data.imgList;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'comment_img_file', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        that.setData({
          imgListSum: i
        })
        let res = JSON.parse(resp.data)
        if(res.status == 1){
          imgList.push(res.result)
          that.setData({
            imgList: imgList,
            isRemoveImg: true
          })
        }else{
         console.log(res.msg)
        }
       

        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    })
  },
  // 上传信息
  issue() {
    const that = this;

    console.log(that.data.imgSum);
    if (that.data.isRemoveImg) {
      var imgSum = that.data.imgSum;
      const imgListLength = that.data.imgList.length;
      const imgStart = imgListLength - (that.data.imgListSum + 1);
      let imgCenter = that.data.imgList.slice(imgStart, imgListLength);
      const token = app.globalData.token;
      that.upFile(imgCenter);
    } else {
      let imgCenter = that.data.imgCenter;
      that.upFile(imgCenter);
    }
  },
  upFile(imgCenter) {
    const that = this;
    const token = app.globalData.token;
    let json = {
      token,
      goods_id: that.data.goodsid,
      order_id: that.data.orderid,
      rec_id: that.data.recid,
      service_rank: that.data.sellerSum,
      deliver_rank: that.data.logisticsSum,
      goods_rank: that.data.describeSum,
      goods_score: that.data.goodsSum,
      is_anonymous: that.data.radioChange,
      content: that.data.placeInput,
      img: imgCenter
    }
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.addEvaluate, jsonData).then(res => {
      if (res.status == 1) {
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        throw res.msg
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    that.setData({
      goodsid: options.goodsid,
      orderid: options.orderid,
      recid: options.recid,
      hiddenLoading: true
    })
  },
  onShow:function(){
    //数据统计
    app.statistics("page/detail/pages/evaluate/evaluate", "添加评价")
  }

})